package util

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"log"
	"strings"
)

func ParseHtml(url string, path string) {
	doc, err := goquery.NewDocument(url)
	if err != nil {
		log.Fatal(err)
	}

	title := doc.Find(".item_title > h1").Text()
	fmt.Println("正在开始下载:", title)

	doc.Find(".content_left > img").Each(func(i int, s *goquery.Selection) {
		img_url, _ := s.Attr("src")
		_img := "https://www.8zipai.com" + img_url

		savePath := path + title
		if !Exists(path) {
			err := Mkdir(path)
			if err != nil {
				log.Fatal(err)
			}
		}
		savePath = strings.Replace(savePath, " ", "", -1)
		savePath = strings.Replace(savePath, "\n", "", -1)
		if !IsDir(savePath) {
			err := Mkdir(savePath)
			if err != nil {
				log.Fatal(err)
			}
		}
		fmt.Printf("正在下载【%s】第【%d】张图片\n", title, i)
		go DownloadImg(savePath+"\\", _img)
	})
}
